package com.cljz.makepassenger.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.model.map.City;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by zhangxutong .
 * Date: 16/08/28
 */

public class CityListAdapter extends RecyclerView.Adapter<CityListAdapter.ViewHolder> {
    protected Context mContext;
    protected List<City> mDatas;
    protected LayoutInflater mInflater;
    private CityCallBack cityCallBack;

    public CityListAdapter(Context mContext, List<City> mDatas, CityCallBack cityCallBack) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        this.cityCallBack = cityCallBack;
        mInflater = LayoutInflater.from(mContext);
    }

    public List<City> getDatas() {
        return mDatas;
    }

    public CityListAdapter setDatas(List<City> datas) {
        mDatas = datas;
        return this;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.item_city_list, parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final City cityBean = mDatas.get(position);
        holder.tvCityName.setText(cityBean.getAreaName());
        holder.tvCityName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cityCallBack.OnItemClick(mDatas.get(position));
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDatas != null ? mDatas.size() : 0;
    }


    public interface CityCallBack {
        void OnItemClick(City city);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_city_name)
        TextView tvCityName;


        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }
}
